package com.pactera.madp.cp.controller.oms.product;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.cp.api.dto.product.ProductAddDto;
import com.pactera.madp.cp.api.dto.product.ProductQueryDto;
import com.pactera.madp.cp.api.dto.product.ProductUpdateDto;
import com.pactera.madp.cp.api.entity.product.ProductEntity;
import com.pactera.madp.cp.common.constant.Constant;
import com.pactera.madp.cp.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 产品管理
 *
 * @author kedongZhang
 * @date 2021/5/26 10:22
 */
@RestController
@AllArgsConstructor
@RequestMapping("/product")
@Api(value = "product", tags = "产品管理")
public class ProductController {

    private final ProductService productService;

    /**
     * 新增产品
     *
     * @param dto 新增产品传输对象
     * @return true/false
     */
    @PostMapping
    @ApiOperation(value = "新增产品")
    public R saveProduct(@Valid @RequestBody ProductAddDto dto){
        return productService.saveProduct(dto);
    }

    /**
     * 分页查询产品
     *
     * @param page 分页对象
     * @param dto 查询产品传输对象
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询产品")
    public R getProductPage(Page page, ProductQueryDto dto){
      return productService.getProductPage(page,dto);
    }

    /**
     * 编辑产品
     *
     * @param dto 编辑产品传输对象
     * @return true/false
     */
    @PutMapping
    @ApiOperation(value = "编辑产品")
    public R updateProduct(@Valid @RequestBody ProductUpdateDto dto){
      return productService.updateProduct(dto);
    }

    /**
     * 根据id删除产品
     *
     * @param productId 产品id
     * @return
     */
    @DeleteMapping("/{productId}")
    @ApiOperation(value = "根据id删除产品")
    @ApiImplicitParams({
            @ApiImplicitParam(name="productId",value="产品id",dataType="Integer")
    })
    public R deleteProductById(@PathVariable Integer productId){
        return R.ok(productService.removeById(productId));
    }

    /**
     * 上架产品
     *
     * @param productId 产品id
     * @return true/false
     */
    @PostMapping("/publish/{productId}")
    @ApiOperation(value = "上架产品")
    @ApiImplicitParams({
            @ApiImplicitParam(name="productId",value="产品id",dataType="Integer")
    })
    public R publishProductById(@PathVariable Integer productId){
        ProductEntity productEntity = new ProductEntity();
        productEntity.setId(productId);
        productEntity.setStatus(Constant.PRODUCT_STATUS_PUBLISH_ON);
        return R.ok(productService.updateById(productEntity));
    }

    /**
     * 下架产品
     *
     * @param productId 产品id
     * @return true/false
     */
    @PostMapping("/publish-off/{productId}")
    @ApiOperation(value = "下架产品")
    @ApiImplicitParams({
            @ApiImplicitParam(name="productId",value="产品id",dataType="Integer")
    })
    public R cancelPublishProductById(@PathVariable Integer productId){
        ProductEntity productEntity = new ProductEntity();
        productEntity.setId(productId);
        productEntity.setStatus(Constant.PRODUCT_STATUS_PUBLISH_OFF);
        return R.ok(productService.updateById(productEntity));
    }

    /**
     * 根据分类id获取上架(已发布)产品列表
     * @param groupId 分类id
     * @return list
     */
    @GetMapping("/published/{groupId}")
    @ApiOperation(value = "根据分类id获取上架(已发布)产品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="groupId",value="产品分类id",dataType="Integer")
    })
    public R getPublishedProductListByGroupId(@PathVariable Integer groupId){
       return productService.getPublishedProductListByGroupId(groupId);
    }

    /**
     * 开启精选/关闭精选
     *
     * @param productId 产品id
     * @return
     */
    @PostMapping("/select/{productId}")
    @ApiOperation(value = "开启精选/关闭精选")
    public R selectProduct(@PathVariable Integer productId){
      return productService.selectProduct(productId);
    }

}
